package org.example.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.erp.domain.IconTable;
import org.example.erp.domain.SysMenu;
import org.example.erp.domain.SysPost;
import org.example.erp.service.impl.IconTableServiceImpl;
import org.example.erp.service.impl.SysMenuServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@RequestMapping("/api")
@RestController
public class MenuController {

    @Autowired
    SysMenuServiceImpl sysMenuService;//菜单表

    @Autowired
    IconTableServiceImpl iconTableService;//菜单图标表

    @GetMapping("/menuManagement/findMenuCascade")//查询级联菜单
    public DataResult findMenuList(
            @RequestParam(required = false) String menuName,
            @RequestParam(required = false) Integer status
    ){
        List<SysMenu> menuCascade = sysMenuService.findMenuCascade(menuName, status);
        return DataResult.success(menuCascade);
    }

    @PostMapping("/menuManagement/insert")
    public DataResult insertMenu(@RequestBody SysMenu sysMenu){
        sysMenu.setCreateTime(new Date());
        sysMenuService.save(sysMenu);
        return DataResult.success();
    }


    @PutMapping("/menuManagement/update")//修改菜单
    public DataResult updatePost(@RequestBody SysMenu sysMenu){
        sysMenu.setUpdateTime(new Date());
        sysMenuService.updateById(sysMenu);
        return DataResult.success();
    }

    @GetMapping("/menuManagement/findIcon")//查询图标
    public DataResult findIconList(
            @RequestParam(required = false) String name
    ){
        QueryWrapper<IconTable> queryWrapper = new QueryWrapper<>();
        if (name!=null && !name.equals("")){
            queryWrapper.lambda().like(IconTable::getName,name);
        }
        List<IconTable> list = iconTableService.list(queryWrapper);
        return DataResult.success(list);
    }

    //查询当前角色拥有哪些功能权限
    @GetMapping("/menuManagement/findBySys_RoleId")
    public DataResult findBySys_RoleId(Integer roleId){
        List<SysMenu> menus = sysMenuService.findBySys_RoleId(roleId);
        return DataResult.success(menus);
    }

    @DeleteMapping("/menuManagement/delete")//菜单删除
    public DataResult delete(Integer menuId){
        boolean remove = sysMenuService.removeById(menuId);
        if (remove) {
            return DataResult.success(remove ? "成功" : "失败");
        }else {
            return DataResult.error("菜单删除失败");
        }
    }
}
